package com.qingke.studentinfo.Controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qingke.studentinfo.Service.StudentService;
import com.qingke.studentinfo.Utils.ConstantUtils;
import com.qingke.studentinfo.pojo.Student;

public class AddStudentServlet extends HttpServlet implements ConstantUtils {
	
	private static final long serialVersionUID = 1L;

	private StudentService ss = new StudentService();
	
	public AddStudentServlet() {
		super();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 设置编码格式，防止表单提交的数据乱码
		req.setCharacterEncoding("UTF-8");
		// 创建一个HttpSssion会话
		HttpSession session = req.getSession();
		String stuName = req.getParameter("stuName");
		Student student = new Student();
		student.setStuName(stuName);
		int result = ss.addStudent(student);

		if (result > 0) {
			session.setAttribute(MSG, MSG_STUDENT_ADD_SUCCESS);
		} else {
			session.setAttribute(MSG, MSG_STUDENT_ADD_FAILED);
		}
		//使用重定向跳转页面
		resp.sendRedirect(req.getContextPath() + "/searchStudent.jsp");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
